﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel; 
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil.CustomObject;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Numeric
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityD0002", Name = "位数检证", CategoryType = EnumCategoryType.数值, Model = true)]
    public class VerfityD0002 : VerfityOneResult
    {
        private CustomCollection<ElementLength> _elementsLength = new CustomCollection<ElementLength>()
                                                                      {Description = "位数列表"};

        /// <summary>
        /// 
        /// </summary>
        [Category("元素预想位数"), Description("Row:行数 Column:列数 Length:预想位数")]
        [TypeConverter(typeof (CustomCollectionConverter<ElementLength>))]
        [RefreshProperties(RefreshProperties.All)]
        public CustomCollection<ElementLength> ElementsLength
        {
            get { return _elementsLength; }
            //set { _elementsLength = value; }
        }

        private bool _igronQuestionMark = true;

        [Category("是否忽略带有？的录入结果"), Description("true:忽略带有？结果 false：不忽略")]
        public bool IgronQuestionMark
        {
            get { return _igronQuestionMark; }
            set { _igronQuestionMark = value; }
        }


        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;

            if (entryCommon == "30"  )
                //entryCommon == "20")
            {
                return false;
            }

            //循环每行
            for (int i = 0; i < result.Count; i++)
            {
                var rowResult = result[i];

                for (int j = 0; j < rowResult.Count; j++)
                {
                    var colResult = rowResult[j];

                    //如果录入结果只是一个？ 忽略
                    if (colResult.Replace("/", string.Empty).Equals("?"))
                    {
                        continue;
                    }

                    //结果为空白
                    if (EmptyResultCount(colResult) == 1)
                    {
                        continue;
                    }

                    bresult = VerfityLenth(i, j, colResult);

                    if (bresult)
                        break;

                }

                if (bresult)
                    break;
            }


            if (bresult)
            {
                System.Console.WriteLine("VerfityD0002"); 
            }
            return bresult;
        }


        private bool VerfityLenth(int row, int col, string colResult)
        {
            bool bresult = false;
            
            // find elementLength
            // 找到 预想位数

            if (ElementsLength == null)
                return false;

            if (_igronQuestionMark && QuestionMarkResultCount(colResult) > 0)
                return false;

            
                List<int> elementLengths = null;

                //X 行 X 列 value
                if (ElementsLength.Count(t => t.Row.Equals(row) && t.Column.Equals(col)) > 0)
                {
                    elementLengths = ElementsLength.Where(t => t.Row.Equals(row) && t.Column.Equals(col)).Select(t => t.Length).ToList();
                }
                    //-1 行 X 列 value   任意行  X 列
                else if (ElementsLength.Count(t => t.Row.Equals(-1) && t.Column.Equals(col)) > 0)
                {
                    elementLengths = ElementsLength.Where(t => t.Row.Equals(-1) && t.Column.Equals(col)).Select(t => t.Length).ToList();

                }
                    //-1 行 -1 列 value   任意行  任意 列
                else if (ElementsLength.Count(t => t.Row.Equals(-1) && t.Column.Equals(-1)) > 0)
                {
                    elementLengths = ElementsLength.Where(t => t.Row.Equals(-1) && t.Column.Equals(-1)).Select(t => t.Length).ToList();

                }

                if (elementLengths != null && elementLengths.Any() )
                {
                    //实际位数
                    //发现元素位数 和预想不一样
                    //抽出
                    int colResultLen = colResult.Replace("/", string.Empty).Length;

                    if ( elementLengths.Contains(colResultLen) == false)
                    {
                        bresult = true;
                    }
                }
            

            return bresult;
        }
    }
 
}